/*
 * Copyright (c) 2012 Aleksander Zotov, Benedikt Markt, Aliaksei Babkovich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 *  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package database;

import java.util.ArrayList;
import java.util.Date;

import simulation.Simulation;
import user.User;
import user.UserManager;
import utils.ApplicationPreferences;
import utils.Debug;
import utils.UserNotFoundException;
/**
 * Placeholder class that emulates a SQL database until SQL management is completed
 * 
 * @author Benedikt Markt
 *
 */
public class DatabaseManager {

	String databaseIP;

	public DatabaseManager() {
		ApplicationPreferences prefs = ApplicationPreferences.getInstance();
		databaseIP = prefs.getDbURL();
	}

	// TODO implement
	public void storeSimulation(Simulation simulation) {
		Debug.log(Debug.DEBUG, "NIY: DatabaseManager.storeSimulation");
	}

	// TODO implement
	public void deleteSimulation(String identifier) {
		Debug.log(Debug.DEBUG, "NIY: DatabaseManager.deleteSimulation");
	}

	// TODO implement
	public ArrayList<Simulation> loadSimulations() {
		Debug.log(Debug.DEBUG, "NIY: DatabaseManager.loadSimulations");
		ArrayList<Simulation> simulations = new ArrayList<Simulation>();
		Date date = new Date();

		String results = "";
		results += "   Reference: GRACE 1             (27391)" + "<br/>";
		results += "   Object FENGYUN 1C DEB      (31730)" + "<br/>";
		results += "   Date: 05 Sep 2012 12:20:56.162006" + "<br/>";
		results += "   Range =   3.0268 km" + "<br/>";
		results += "   Relative Velocity =   9.1972 km/s" + "<br/>";
		String refData[] = new String[3];
		refData[0] = "0 GRACE 1";
		refData[1] = "1 00005U 58002B   12363.15598079  .00000379  00000-0  48037-3 0   286";
		refData[2] = "1 00005 034.2514 350.0193 1846771 319.2698 028.3238 10.84179820909104";
		try {
			Simulation sim1 = new Simulation("Simulation 1",
					"Bsp-Beschreibung 1", 5, date, date, refData, UserManager
							.getInstance().getUser("Admin"));
			sim1.setResults(results, 1);
			Simulation sim2 = new Simulation("Simulation 2",
					"Bsp-Beschreibung 2", 3, date, date, refData, UserManager
							.getInstance().getUser("Admin"));
			sim2.setResults(results, 1);
			Simulation sim3 = new Simulation("Simulation3",
					"Bsp-Beschreibung 3", 2, date, date, refData, UserManager
							.getInstance().getUser("Admin"));
			sim3.setResults(results, 1);
			simulations.add(sim1);
			simulations.add(sim2);
			simulations.add(sim3);
			return simulations;
		} catch (UserNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	// TODO add to draft, implement
	public ArrayList<User> loadUsers() {

		ArrayList<User> users = new ArrayList<User>();

		// Example Users. delete when DB is ready!
		// begin DEBUG
		try {
			User user1 = new User("Admin");

			user1.setPassword("SatAdmin1*");
			user1.setAdminState(true);

			users.add(user1);

		} catch (Exception e) {
			e.printStackTrace();
		}

		// end DEBUG

		return users;

	}

	/**
	 * Adds a new user, if it doesn't exist yet, or updates user data if user
	 * exists
	 * 
	 * @param user
	 *            TODO implement
	 */
	public void storeUser(User user) {
		Debug.log(Debug.DEBUG, "NIY: DatabaseManager.storeUser");
	}

	// TODO implement
	public void deleteUser(User user) {
		Debug.log(Debug.DEBUG, "NIY: DatabaseManager.deleteUser");
	}

	/**
	 * Checks if the database server is available
	 * 
	 * @return TODO implement
	 */
	public boolean isAvailable() {
		Debug.log(Debug.DEBUG, "NIY: DatabaseManager.isAvailable");
		return false;
	}

}
